package tour.general.stage;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import tour.general.BaseActivity;
import tour.general.R;
import tour.general.adapter.StagesAdapter;
import tour.general.stage.zoom.StagesTotalZoomActivity;
import tour.general.util.Commons;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class StagesActivity extends BaseActivity {
	private JSONArray mStagesArray = null;
	private StagesAdapter mStagesAdapter;
	private ListView mStagesList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.stages);
		initToolbar();

		mStagesArray = Commons.getInstance().getStagesArray();

		mStagesAdapter = new StagesAdapter(this, mStagesArray);
		mStagesList = (ListView) findViewById(R.id.list_stages);
		mStagesList.setAdapter(mStagesAdapter);

		mStagesList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				Intent intent = new Intent(StagesActivity.this, StageDetailActivity.class);
				JSONObject jsonObject = (JSONObject) mStagesAdapter.getItem(position);
				String tappa = "";

				try {
					tappa = jsonObject.getString("tappa");
					if (tappa.equals("Riposo")) {
						return;
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}
				int i = Integer.parseInt(tappa);
				Commons.getInstance().setSelectTappaPosition(i);

				startActivity(intent);
			}
		});

		ImageButton totaleStages= (ImageButton)findViewById(R.id.total_stages_button);
		totaleStages.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(StagesActivity.this, StagesTotalZoomActivity.class);
				startActivity(intent);
				
			}
		});
	}

}
